import { IComponent, IWindowSizeChange, IAfterViewInit } from '../../core/component';

// 顶点着色器
// 顶点坐标传入了顶点着色器，顶点着色器根据传入的gl.POINTS/gl.LINES/gl.TRIANGLES参数，进行图元装配
var VSHADER_SOURCE =`
    // vec4 就是包含4个元素的浮点型向量(坐标)；
    // attribute 修饰符，是由浏览器（javascript）传输给顶点着色器的变量值修饰符；
    attribute vec4 a_Position;
    void main() { 
        //设置坐标 position 即我们定义的顶点坐标，传入到着色器的；gl_Position 是一个内建的传出变量。
        gl_Position = a_Position; 
    }
`;

//片元着色器
var FSHADER_SOURCE =`
    void main() {
        //设置颜色
        gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
    }
`

function initBuffers(gl: WebGLRenderingContext, shaderProgram: any) {
    var vertices = new Float32Array([
        0.0, 0.5, -0.5, -0.5, 0.5, -0.5
    ]);
    var n = 3;//点的个数
    //创建缓冲区对象
    var vertexBuffer = gl.createBuffer();
    if (!vertexBuffer) {
        console.log("Failed to create the butter object");
        return -1;
    }
    //将缓冲区对象绑定到目标
    gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
    //向缓冲区写入数据
    gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
    //获取坐标点
    var a_Position = gl.getAttribLocation(shaderProgram, 'a_Position');
    //将缓冲区对象分配给a_Position变量
    gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0);
    //连接a_Position变量与分配给它的缓冲区对象
    gl.enableVertexAttribArray(a_Position);
    return n;
}
export class S01 implements IComponent, IWindowSizeChange, IAfterViewInit {
    protected vs: string = `
        attribute vec4 a_Position;
        void main() { 
            gl_Position = a_Position; 
        }
    `;
    protected fs: string = `
        void main() {
            //设置颜色
            gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
        }
    `;
    private canvasEle: HTMLCanvasElement;
    private gl: WebGLRenderingContext;
    constructor(container: HTMLElement, id: any) {
        this.canvasEle = document.createElement('canvas') as HTMLCanvasElement;
        let height = container.clientHeight;
        let width = container.clientWidth;
        this.canvasEle.setAttribute('height', height.toString());
        this.canvasEle.setAttribute('width', width.toString());
        container.append(this.canvasEle);
        this.gl = this.canvasEle.getContext('webgl');
        // this.gl.POINTS;
        // this.gl.LINES;
        // this.gl.TRIANGLES;
    }
    public init() {

    }
    
    public afterviewInit() {
        //编译着色器
        var vertShader = this.gl.createShader(this.gl.VERTEX_SHADER);
        this.gl.shaderSource(vertShader, this.vs);
        this.gl.compileShader(vertShader);

        //编译着顶点/片段着色器
        var fragShader = this.gl.createShader(this.gl.FRAGMENT_SHADER);
        this.gl.shaderSource(fragShader, this.fs);
        this.gl.compileShader(fragShader);

        //合并程序
        var shaderProgram = this.gl.createProgram();
        this.gl.attachShader(shaderProgram, vertShader);
        this.gl.attachShader(shaderProgram, fragShader);
        this.gl.linkProgram(shaderProgram);
        this.gl.useProgram(shaderProgram);

        //获取坐标点
        var a_Position = this.gl.getAttribLocation(shaderProgram, 'a_Position');
        if (a_Position < 0) {
            console.log('Failed to get the storage location of a_Position');
            return;
        }

        var n = initBuffers(this.gl, shaderProgram);

        if (n < 0) {
            console.log('Failed to set the positions');
            return;
        }
        // 清除指定<画布>的颜色
        this.gl.clearColor(0.0, 0.0, 0.0, 1.0);

        // 清空 <canvas>
        this.gl.clear(this.gl.COLOR_BUFFER_BIT);

        this.gl.drawArrays(this.gl.TRIANGLES, 0, n);
    }
    public render(): HTMLElement {
        return null;
    }
    public windowSizeChange(ev: any) {
        console.log(ev)
    }
}